package com.servlet;

import com.bean.GoodsBean;
import com.google.gson.Gson;
import com.service.GoodsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/GoodsServlet")
public class GoodsServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setHeader("Content-type","application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        GoodsService goodsService = new GoodsService();

        try {
            List<GoodsBean> goodsBeans = goodsService.selRow();
            Gson gson = new Gson();
            String json = gson.toJson(goodsBeans);//将字符串转换成json，前端只接受json数据。
            out.println(json);
        } catch (SQLException e) {
            e.printStackTrace();
            out.println("查询员工失败"+e.getMessage());
        }





    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
